package com.syqc.monitor.ui.tmn

import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.entity.TmnManager
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper

/**
 *@author joker
 *@time   2024/3/13 9:42
 *@desc
 */
class DeleteTmnVM : AutoDisposeViewModel() {
    private val tmnIds = ArrayList<String>()
    val tmnsLive = MutableLiveData<ArrayList<TmnManager>>()
    var teamId: String = ""
    val tmns = ArrayList<TmnManager>()
    val deleteLive = MutableLiveData<Boolean>()
    var isChange = false

    fun isDelete(managers: MutableList<TmnManager>): Boolean {
        tmnIds.clear()
        managers.forEach {
            if (it.isChoose) tmnIds.add(it.tmnId)
        }
        if (tmnIds.isEmpty()) {
            toast(R.string.toast_choose_tmn)
            return true
        }
        return false
    }

    //删除终端
    fun deleteTmn() {
        toastLoading(true)
        var tmns = ""
        tmnIds.forEach {
            tmns = "$tmns$it,"
        }
        if (tmns.endsWith(",")) tmns = tmns.substring(0, tmns.length - 1)
        ServiceHelper.getRepository().deleteTmn(tmns).autoDispose(this).subscribe({
            toastLoading(false)
            if (it.status == 1) {
                toast(R.string.toast_delete_success)
                deleteLive.postValue(true)
                isChange = true

            } else {
                toast(R.string.toast_delete_faile)
            }
        }, error)
    }

    //获取终端
    fun getTmnByTeam() {
        toastLoading(true)
        ServiceHelper.getRepository().getTmnByTeam(teamId).autoDispose(this).subscribe({
            toastLoading(false)
            tmns.clear()
            tmns.addAll(it)
            tmnsLive.postValue(it)
        }, error)
    }

    //全选
    fun allCheck() {
        tmns.forEach {
            it.isChoose = true
        }
        tmnsLive.postValue(tmns)
    }

    //全不选
    fun allUnCheck() {
        tmns.forEach {
            it.isChoose = false
        }
        tmnsLive.postValue(tmns)
    }


}